import 'package:common/module/module_manager.dart';
import 'package:common/module/module_registrar.dart';
import 'package:common/preference/t_preferences.dart';
import 'package:common/common.dart';

class Environment {
  static final String SWITCH_ENVIRONMENT = "switch_environment";
  static final String Test = "Test";
  static final String Pre = "Pre";
  static final String Release = "Release";

  static setEnv(BuildType buildType) async {
    String env = "";
    if (buildType == null) {
      env = Release;
    }
    if (BuildType.Test == buildType) {
      env = Test;
    } else if (BuildType.Pre == buildType) {
      env = Pre;
    } else {
      env = Release;
    }
    await TPreferences().get.then((value) => value?.setString(Environment.SWITCH_ENVIRONMENT, env));
  }

  static Future<String> getEnv() async {
    var preferences = await TPreferences().get;
    var env = preferences?.get(Environment.SWITCH_ENVIRONMENT);
    return env;
  }

  static initEnv(BuildType buildType) async {
    await TPreferences().get.then((prefs) {
      final envStr = prefs?.getString(Environment.SWITCH_ENVIRONMENT);
      if (TUtil.isEmptyString(envStr)) {
        ModuleManager.init(buildType);
      } else {
        if (Environment.Test == envStr) {
          ModuleManager.init(BuildType.Test);
        } else if (Environment.Pre == envStr) {
          ModuleManager.init(BuildType.Pre);
        } else {
          ModuleManager.init(BuildType.Release);
        }
      }
    });
  }

}
